﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace fApp.Forms
{
    public partial class fControl : UserControl
    {
        public fControl()
        {
            InitializeComponent();
        }

        private Color _BorderColor;
        [Category("表示")]
        [DefaultValue(typeof(Color), "Black")]
        [Description("コントロールの境界線色を取得または設定します。")]
        public Color BorderColor
        {
            get { return this._BorderColor; }
            set
            {
                this._BorderColor = value;
                this.Invalidate();
            }
        }

        private BorderStyle _BorderStyle;
        [Category("表示")]
        [DefaultValue(typeof(BorderStyle), "None")]
        [Description("コントロールの境界線スタイルを取得または設定します。")]
        public new BorderStyle BorderStyle
        {
            get { return this._BorderStyle; }
            set
            {
                this._BorderStyle = value;
                this.Invalidate();
            }
        }

        private int _BorderWidth;
        [Category("表示")]
        [DefaultValue(1)]
        [Description("コントロールの境界線の幅を取得または設定します。")]
        public int BorderWidth
        {
            get { return this._BorderWidth; }
            set
            {
                this._BorderWidth = value;
                this.Invalidate();
            }
        }

        private int _RadiusTopLeft;
        [Category("表示")]
        [DefaultValue(5)]
        [Description("コントロールの左上の角の半径を取得または設定します。")]
        public int RadiusTopLeft
        {
            get { return this._RadiusTopLeft; }
            set
            {
                this._RadiusTopLeft = value;
                this.Invalidate();
            }
        }

        private int _RadiusTopRight;
        [Category("表示")]
        [DefaultValue(5)]
        [Description("コントロールの右上の角の半径を取得または設定します。")]
        public int RadiusTopRight
        {
            get { return this._RadiusTopRight; }
            set
            {
                this._RadiusTopRight = value;
                this.Invalidate();
            }
        }

        private int _RadiusBottomLeft;
        [Category("表示")]
        [DefaultValue(5)]
        [Description("コントロールの左下の角の半径を取得または設定します。")]
        public int RadiusBottomLeft
        {
            get { return this._RadiusBottomLeft; }
            set
            {
                this._RadiusBottomLeft = value;
                this.Invalidate();
            }
        }

        private int _RadiusBottomRight;
        [Category("表示")]
        [DefaultValue(5)]
        [Description("コントロールの右下の角の半径を取得または設定します。")]
        public int RadiusBottomRight
        {
            get { return this._RadiusBottomRight; }
            set
            {
                this._RadiusBottomRight = value;
                this.Invalidate();
            }
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle ar = this.ClientRectangle;
            ar.X += ar.X + Convert.ToInt32(Math.Floor((double)this.BorderWidth / 2));
            ar.Y += ar.Y + Convert.ToInt32(Math.Floor((double)this.BorderWidth / 2));
            ar.Width -= (this.BorderWidth + 1);
            ar.Height -= (this.BorderWidth + 1);
            Rectangle lr = this.ClientRectangle;
            bool canArc = ((ar.Width > 0) && (ar.Height > 0));

            using (System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath())
            {
                gp.StartFigure();
                if ((this.RadiusTopRight > 0) && (canArc == true))
                {
                    int w = this.RadiusTopRight > ar.Width ? ar.Width : this.RadiusTopRight;
                    int h = this.RadiusTopRight > ar.Height ? ar.Height : this.RadiusTopRight;
                    gp.AddArc(ar.Right - w, ar.Top, w, h, 270, 90);
                }
                else
                {
                    gp.AddLine(lr.Right, lr.Top, lr.Right, lr.Top);
                }
                if ((this.RadiusBottomRight > 0) && (canArc == true))
                {
                    int w = this.RadiusBottomRight > ar.Width ? ar.Width : this.RadiusBottomRight;
                    int h = this.RadiusBottomRight > ar.Height ? ar.Height : this.RadiusBottomRight;
                    gp.AddArc(ar.Right - w, ar.Bottom - h, w, h, 0, 90);
                }
                else
                {
                    gp.AddLine(lr.Right, lr.Bottom, lr.Right, lr.Bottom);
                }
                if ((this.RadiusBottomLeft > 0) && (canArc == true))
                {
                    int w = this.RadiusBottomLeft > ar.Width ? ar.Width : this.RadiusBottomLeft;
                    int h = this.RadiusBottomLeft > ar.Height ? ar.Height : this.RadiusBottomLeft;
                    gp.AddArc(ar.Left, ar.Bottom - h, w, h, 90, 90);
                }
                else
                {
                    gp.AddLine(lr.Left, lr.Bottom, lr.Left, lr.Bottom);
                }
                if ((this.RadiusTopLeft > 0) && (canArc == true))
                {
                    int w = this.RadiusTopLeft > ar.Width ? ar.Width : this.RadiusTopLeft;
                    int h = this.RadiusTopLeft > ar.Height ? ar.Height : this.RadiusTopLeft;
                    gp.AddArc(ar.Left, ar.Top, w, h, 180, 90);
                }
                else
                {
                    gp.AddLine(lr.Left, lr.Top, lr.Left, lr.Top);
                }
                gp.CloseFigure();

                if (canArc == true)
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                }
                // 背景を描画
                using (SolidBrush sb = new SolidBrush(this.BackColor))
                {
                    e.Graphics.FillPath(sb, gp);
                }
                // 境界線を描画
                if (this.BorderStyle != BorderStyle.None)
                {
                    using (Pen p = new Pen(this.BorderColor, this.BorderWidth))
                    {
                        e.Graphics.DrawPath(p, gp);
                    }
                }
                if (canArc == true)
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                }
               
            }
            base.OnPaint(e);
        }
    }
}
